<?php
	function getItemClick($itemid){
		//echo "itemid: $itemid";
		$html = file_get_contents("http://taoke.alimama.com/spreader/gen_single_code.htm?pid=$itemid");
		if(preg_match("/http:\/\/s\.click\.alimama\.com\/a\/.*?=-/",$html,$match )){
			//echo $match[0];
			//return $match[0];
			//header("Location: $match[0]11575228");
			//echo "$match[0]11575228";
			//return true;
			return "$match[0]11575228";
		}
		else{
			return false;
		}
	}
	function getStoreClick($storeid){
		//echo "sotreid: $sotreid";
		$html = file_get_contents("http://taoke.alimama.com/spreader/gen_shop_code.htm?pid=$storeid");
		//echo $html;
		//http://s.click.alimama.com/a/qXw58kgBhL0=-10011550
		if(preg_match("/http:\/\/s\.click\.alimama\.com\/a\/.*?=-/",$html,$match )){
			//echo $match[0];
			//return $match[0];
			//header("Location: $match[0]11575228");
			//echo "$match[0]11575228";
			return "$match[0]11575228";
		}
		else{
			//header("Location: .");
			//echo "/";
			return false;
		}		
	}
	
	//$url_o = $_GET['u'];
	$url_o = substr($_SERVER["QUERY_STRING"],2);
	//echo "$url_o $url_o";
	$path = parse_url($url_o);
	$path = $path["path"];
	$path = substr($path,1);
	//echo $path;
	if($path== '' || preg_match("/index/",$path) ){
		$faq = "";
	}
	else if(preg_match("/search/",$path) ){		
		$faq = substr($path,7) ;
		$faq = base64_decode($faq);
	}
	else if(preg_match("/taobao/",$path) ){		
		$faq = substr($path,7) ;
		$faq = base64_decode($faq);
		//echo " faq $faq";
	}
	else{
		$faq = base64_decode($path);
	}
	//echo ' faq='.$faq;
	//echo $url_o;
	//跳转
	if(preg_match("/pid=(\d+)/",$faq,$match)){
		//echo " pid $faq";
		/*if(false == getItemClick($match[1])){
			if(preg_match("/sid=(\d+)/",$faq,$match)){
				getStoreClick($match[1]);
			}
			else
				echo 'shit';
		}*/
		$re = getItemClick($match[1]);
		if($re == false){
			if(preg_match("/sid=(\d+)/",$faq,$match)){
				$re = getStoreClick($match[1]);
			}
			else
				$re = false;
		}
		if($re==false)
			echo "/";
		else{
			echo $re;
			//<meta http-equiv="refresh" content="3;url=http://192.168.102.28/bbs">
			$re = "<meta http-equiv=\"refresh\" content=\"0;url=$re\">";
			file_put_contents($path,$re);
		}
			
		
	}
	else if(preg_match("/sid=(\d+)/",$faq,$match)){
		//echo " sid $faq";
		//echo ",2";
		//taoapi.aspx?sid=11083790&c=50005844
		$re = getStoreClick($match[1]);
		if($re==false)
			echo "/";
		else{
			echo $re;
			$re = "<meta http-equiv=\"refresh\" content=\"0;url=$re\">";
			file_put_contents($path,$re);
		}
	}
	else{
		if(preg_match("/search/",$path)){
			//搜索
			//http://localhost/search/Yz0yJms9gAmYT0yJm09Mg==
			//$url = str_replace($path.'/search/', 'http://www.taobangwang.cn/searchp.aspx?', $url_o);
			//http://localhost/search/c=2&k=%E7%9A%84%E9%A3%8E%E6%A0%BC&a=1&m=3
			//echo "http://www.taobangwang.cn/searchp.aspx?c=$category&k=$key&a=$address&m=$credit";
			//$url = "http://www.taobangwang.cn/searchp.aspx?c=$category&k=$key&a=$address&m=$credit";
			//echo "$url";
			$file =  $path;
			//echo $url_o;
			//echo $para;
			//echo ',file '.$file;
			//echo ',faq '.$faq;
			$url = "http://www.taobangwang.cn/searchp.aspx?$faq";
			//echo ',url '.$url;
		}
		else{
			//$url = str_replace($path, 'http://www.taobangwang.cn', $url_o);
			$url = "http://www.taobangwang.cn/$faq";
			//echo ' url:'.$url;
			//echo "$url";
			if( $path=="")
				$file="index.html";
			else
				$file =  $path;
		}
				
		require_once 'simple_html_dom.php';
		$html = file_get_html($url);
		foreach($html->find('a') as $element){
			//http://www.taobangwang.cn/taoapi.aspx?pid=100781&sid=10143153&c=50011397
			if(preg_match("/taoapi\.aspx\?(.+)/",$element->href,$match)){
				$a = base64_encode($match[1]);
				$element->href = "/taobao/$a";
			}
			else if(preg_match("/javascript/",$element->href) ){
				$element->outertext = $element->plaintext;
			}
			else{
				$a = base64_encode($element->href);
				$element->href = $a;
			}
		}
		
		$gets = $html->find("div[id=Category]");
		//<span id="ctl00_SiteMapPath1">
		$gets = array_merge($gets,$html->find('span[id=ctl00_SiteMapPath1]'));
		//<span class="__mozilla-findbar-search" 
		//$gets = array_merge($gets,$html->find('span[class=__mozilla-findbar-search]'));
		//$gets = array_merge($gets,$html->find("div[class=listall]"));
		$gets = array_merge($gets,$html->find("table[class=GridViewStyle]"));
		
		$s = $html->find("div[class=listall]",1);
		//<div id="pnlTaobao" class="search">
		$keyword = $html->find("div[id=pnlTaobao] a",0)->plaintext;
		$s = $s."<div><h3>$keyword</h3><div>";
		
		foreach($gets as $g) {
			$s = $s.$g;
		}
		if(strlen($s)>200){
$head = <<<EOD
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html lang="zh-CN">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link type="text/css" rel="stylesheet" href="/css/main.css" />
	<link type="text/css" rel="stylesheet" href="/css/gridview.css" />
</head>
<body><div id="up"></div>
EOD;
$tail = <<<EOD
<div id="down"></div>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/add.js"></script>
</body></html>
EOD;
			file_put_contents($file,$head.$s.$tail);
			//echo $s;
			echo 'ok';
		}
		else{
			echo "/";
		}
			
	}
?>